package com.ylx.web.g;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
    protected void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        System.out.println(name);
        //输出在浏览器
        try {
//               通过输入流读文件
            FileInputStream fileInputStream=new FileInputStream("C:\\Users\\FX95\\IdeaProjects\\login\\src\\main\\webapp\\upload\\"+name);
            ServletOutputStream outputStream = response.getOutputStream();
            int len=0;
            byte[] bytes=new byte[1024];

            while((len=fileInputStream.read(bytes))!=-1){

                outputStream.write(bytes);
                outputStream.flush();
            }

            fileInputStream.close();
            outputStream.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);

    }
}
